﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using WebStudio.Business.Entity;
using WebStudio.Framework.Helper;

namespace WebStudio.Business.Rule
{
    public class CategoryRule
    {
        #region 公有方法
        /// <summary>
        /// 获取信息列表
        /// </summary>
        /// <returns></returns>
        public static List<CategoryEntity> GetCategoryList()
        {
            List<CategoryEntity> list = new List<CategoryEntity>();
            using (var dr = DBHelper.ExecuteReader(string.Format("SELECT * FROM CategoryTB")))
            {
                return FillDate(dr);
            }
        }
        public static List<CategoryEntity> GetContentCategoryList()
        {
            List<CategoryEntity> list = new List<CategoryEntity>();
            using (var dr = DBHelper.ExecuteReader(string.Format("SELECT * FROM CategoryTB where CategoryId<>2 and CategoryId<>3 and ParentId=0")))
            {
                return FillDate(dr);
            }
        }
        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="pageIndex">第几页</param>
        /// <param name="pageSize">每页显示的数目</param>
        /// <param name="pageCount">总共多少页</param>
        /// <param name="recordCount">公共记录数</param>
        /// <returns></returns>
        public static List<CategoryEntity> GetCategoryList(int pageIndex, int pageSize, int pageCount, int recordCount, string strWhere)
        {
            List<CategoryEntity> list = new List<CategoryEntity>();
            string strSql = string.Empty;
            if (pageIndex >= pageCount)
            {
                if (recordCount == 0)
                {
                    strSql = "select * from (select top " + pageSize + " * from (select top " + pageIndex * pageSize + " s.*,p.CategoryName as PCategoryName FROM CategoryTB s left join CategoryTB p on s.ParentId=p.CategoryId where " + strWhere + " order by s.CategoryId desc) order by CategoryId) order by CategoryId desc";
                }
                else
                {
                    strSql = "select * from (select top " + (recordCount - (pageIndex - 1) * pageSize) + " * from (select top " + pageIndex * pageSize + " s.*,p.CategoryName as PCategoryName FROM CategoryTB s left join CategoryTB p on s.ParentId=p.CategoryId where " + strWhere + " order by s.CategoryId desc) order by CategoryId) order by CategoryId desc";
                }
            }
            else
            {
                strSql = "select * from (select top " + pageSize + " * from (select top " + pageIndex * pageSize + " s.*,p.CategoryName as PCategoryName FROM CategoryTB s left join CategoryTB p on s.ParentId=p.CategoryId where " + strWhere + " order by s.CategoryId desc) order by CategoryId) order by CategoryId desc";
            }
            using (var dr = DBHelper.ExecuteReader(strSql))
            {
                return DeepFillData(dr);
            }
        }
        /// <summary>
        /// 获得符合条件的记录数
        /// </summary>
        /// <returns></returns>
        public static int GetCategoryListCount(string strWhere)
        {
            return DBHelper.ExecuteIntScalar(string.Format("select count(*) as allcounts from CategoryTB where {0} ", strWhere));
        }
        /// <summary>
        /// 根据条件获取信息列表
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        public static List<CategoryEntity> GetCategoryListByCondition(string condition)
        {
            using (var dr = DBHelper.ExecuteReader(string.Format("SELECT * FROM CategoryTB WHERE {0}", condition)))
            {
                return FillDate(dr);
            }
        }

        /// <summary>
        /// 获取信息实体
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        public static CategoryEntity GetCategoryEntity(string condition)
        {
            using (var dr = DBHelper.ExecuteReader(string.Format("SELECT * FROM CategoryTB WHERE {0}", condition)))
            {
                List<CategoryEntity> list = FillDate(dr);
                return list == null || list.Count == 0 ? (new CategoryEntity()) : list[0];
            }
        }

        /// <summary>
        /// 根据标识获取信息实体重载一
        /// </summary>
        /// <param name="messageId"></param>
        /// <returns></returns>
        public static CategoryEntity GetCategoryEntity(int categoryId)
        {
            return GetCategoryEntity(string.Format("CategoryId = {0}", categoryId));
        }

        public static CategoryEntity GetDeepCategoryEntity(int categoryId)
        {
            using (var dr = DBHelper.ExecuteReader(string.Format("SELECT s.*,p.ParentId as PParentId,p.CategoryName as PCategoryName FROM CategoryTB as s, CategoryTB as p where s.ParentId=p.CategoryId and s.CategoryId = {0}", categoryId)))
            {
                List<CategoryEntity> list = DeepFillData(dr);
                return list == null || list.Count == 0 ? (new CategoryEntity()) : list[0];
            }
        }
        /// <summary>
        /// 保存信息
        /// </summary>
        /// <param name="te"></param>
        /// <returns></returns>
        public static bool SaveCategoryInfor(CategoryEntity te)
        {
            int i = 0;
            if (te.CategoryId <= 0)
            {
                i = DBHelper.ExecuteNonQuery(string.Format("INSERT INTO CategoryTB (CategoryName,ParentId,CategoryDesc,InnerCode) VALUES('{0}',{1},'{2}','{3}')", te.CategoryName, te.CategoryInfo.CategoryId, te.CategoryDesc,te.InnerCode));
            }
            else
            {
                i = DBHelper.ExecuteNonQuery(string.Format("UPDATE CategoryTB SET CategoryName ='{0}' ,ParentId ={1},CategoryDesc='{2}',InnerCode='{4}',SubCatergoryCount={5} WHERE CategoryId = {3}", te.CategoryName, te.CategoryInfo.CategoryId, te.CategoryDesc, te.CategoryId, te.InnerCode,te.SubCatergoryCount));
            }
            return i > 0 ? true : false;
        }

        /// <summary>
        /// 删除信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static bool DeleteCategoryInfor(int categoryId)
        {
            return DBHelper.ExecuteNonQuery(string.Format("DELETE FROM CategoryTB WHERE CategoryId = {0}", categoryId)) >= 0 ? true : false;
        }

        /// <summary>
        /// 获取根父级
        /// </summary>
        /// <param name="pid">父id</param>
        /// <returns></returns>
        public static List<CategoryEntity> GetParentCategoryList(int pid)
        {
            List<CategoryEntity> list = new List<CategoryEntity>();
            string strSql = string.Empty;
            strSql = "select * from CategoryTB where ParentId=" + pid + "";
            using (var dr = DBHelper.ExecuteReader(strSql))
            {
                return FillDate(dr);
            }
        }

        public static List<CategoryEntity> GetDeepParentCategoryList(int pid)
        {
            List<CategoryEntity> list = new List<CategoryEntity>();
            string strSql = string.Empty;
            strSql = "select s.* from CategoryTB s,CategoryTB p where s.ParentId=p.CategoryId and p.ParentId=" + pid + "";
            using (var dr = DBHelper.ExecuteReader(strSql))
            {
                return FillDate(dr);
            }
        }
        #endregion

        #region 私有方法
        /// <summary>
        /// 填充数据
        /// </summary>
        /// <param name="dr"></param>
        /// <returns></returns>
        private static List<CategoryEntity> FillDate(IDataReader dr)
        {
            List<CategoryEntity> list = new List<CategoryEntity>();
            CategoryEntity te;
            while (dr != null && dr.Read())
            {
                te = new CategoryEntity();
                te.CategoryId = ConvertHelper.IntParse(dr["CategoryId"]);
                te.CategoryName = ConvertHelper.StringParse(dr["CategoryName"]);
                te.CategoryInfo.CategoryId = ConvertHelper.IntParse(dr["ParentId"]);
                te.CategoryDesc = ConvertHelper.StringParse(dr["CategoryDesc"]);
                te.SubCatergoryCount = ConvertHelper.IntParse(dr["SubCatergoryCount"]);
                te.InnerCode = ConvertHelper.StringParse(dr["InnerCode"]);
                list.Add(te);
            }
            return list;
        }

        private static List<CategoryEntity> DeepFillData(IDataReader dr)
        {
            List<CategoryEntity> list = new List<CategoryEntity>();
            CategoryEntity te;
            while (dr != null && dr.Read())
            {
                te = new CategoryEntity();
                te.CategoryId = ConvertHelper.IntParse(dr["CategoryId"]);
                te.CategoryName = ConvertHelper.StringParse(dr["CategoryName"]);
                te.CategoryInfo.CategoryId = ConvertHelper.IntParse(dr["ParentId"]);
                te.CategoryInfo.CategoryName = ConvertHelper.StringParse(dr["PCategoryName"]);
                te.CategoryDesc = ConvertHelper.StringParse(dr["CategoryDesc"]);
                te.SubCatergoryCount = ConvertHelper.IntParse(dr["SubCatergoryCount"]);

                list.Add(te);
            }
            return list;
        }
        #endregion
    }
}
